/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.pool2;


/**
 * 池对象工厂(PooledObjectFactory接口):用来创建池对象, 将不用的池对象进行钝化(passivateObject),
 * 对要使用的池对象进行激活(activeObject), 对池对象进行验证(validateObject), 对有问题的池对象进行销毁(destroyObject)等工作
 * <p>
 * PooledObjectFactory是一个池化对象工厂接口，定义了生成对象、激活对象、钝化对象、销毁对象的方法
 */

public interface PooledObjectFactory<T> {

    /**
     * 创建实例,创建一个可以由池提供服务的实例，并将其封装在由池管理的｛@link PooledObject｝中。
     */
    PooledObject<T> makeObject() throws Exception;

    /**
     * 验证实例,确保实例对象是否可用。
     */
    boolean validateObject(PooledObject<T> p);

    /**
     * 激活实例,重新初始化池返回的实例
     */
    void activateObject(PooledObject<T> p) throws Exception;

    /**
     * 钝化实例,取消初始化要返回到空闲对象池的实例。
     */
    void passivateObject(PooledObject<T> p) throws Exception;

    /**
     * 销毁实例,使用默认 (NORMAL)DestroyMode 销毁池不再需要的实例。
     */
    void destroyObject(PooledObject<T> p) throws Exception;

    /**
     * 销毁实例,使用提供的DestroyMode销毁池不再需要的实例。
     */
    default void destroyObject(final PooledObject<T> p, final DestroyMode destroyMode) throws Exception {
        destroyObject(p);
    }

}
